package leetcode.year2021.month10;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class _09_01LetterCombinations17 {
  public List<String> letterCombinations(String digits) {
    ArrayList<String> combinations = new ArrayList<>();

    if (digits.length() == 0){
      return combinations;
    }

    HashMap<Character, String> phoneMap = new HashMap<>();
    phoneMap.put('2', "abc");
    phoneMap.put('3', "def");
    phoneMap.put('4', "ghi");
    phoneMap.put('5', "jkl");
    phoneMap.put('6', "mno");
    phoneMap.put('7', "pqrs");
    phoneMap.put('8', "tuv");
    phoneMap.put('9', "wxyz");

    int length = digits.length();

    int index = 0;

    StringBuffer stringBuffer = new StringBuffer();

    getCombineList(digits, combinations, phoneMap, index,stringBuffer);

    return combinations;
  }

  public void getCombineList(String digits, ArrayList<String> combinations, HashMap<Character, String> phoneMap, int index, StringBuffer stringBuffer){
    if (index == digits.length()){
      combinations.add(stringBuffer.toString());
    } else {
      char[] chars = phoneMap.get(digits.charAt(index)).toCharArray();
      for (int i = 0; i < chars.length; i++) {
        stringBuffer.append(chars[i]);
        getCombineList(digits,combinations,phoneMap,index+1,stringBuffer);
        stringBuffer.deleteCharAt(index);;
      }
    }
  }

  /**
   *定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
   *
   * 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
   *
   * 示例 1：
   *
   * 输入：digits = "23"
   * 输出：["ad","ae","af","bd","be","bf","cd","ce","cf"]
   * 示例 2：
   *
   * 输入：digits = ""
   * 输出：[]
   * 示例 3：
   *
   * 输入：digits = "2"
   * 输出：["a","b","c"]
   *
   */
}
